﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

namespace centraldba_dbrestore_utility
{
    public class ErrorLogging
    {
        /// <summary>
        /// Display an Error Message Popup Window and log the error to the eventlog
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="errorCaption"></param>
        public static void LogEventMessage(Exception ex, string errorCaption)
        {
            // Build the eventlog entry
            LogEntry logEntry = new LogEntry();
            logEntry.EventId = 100;
            logEntry.Severity = System.Diagnostics.TraceEventType.Warning;
            logEntry.Message = ex.Message;
            logEntry.Title = errorCaption;
            logEntry.AddErrorMessage(ex.Source);
            logEntry.AddErrorMessage(ex.StackTrace);

            // Add the error to the eventlog
            var myLogWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            myLogWriter.Write(logEntry);
        }
    }
}
